package com.kzj.fun.interfaces.impl;

import com.kzj.common.log.Logger;
import com.kzj.fun.container.root.RootPanel;
import com.kzj.fun.interfaces.MenuFunction;
import com.kzj.fun.utils.ImageUtil;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

public class PaintBrushPaintFunction implements MenuFunction {
    private RootPanel rootPanel;
    private Point lastPoint;
    public PaintBrushPaintFunction(RootPanel rootPanel){
        this.rootPanel=rootPanel;
    }
    @Override
    public void mousePressed(MouseEvent e) {
        BufferedImage copy = ImageUtil.deepCopy(rootPanel.imageHistory.peek());
        Graphics g = copy.getGraphics();
        if(g instanceof  Graphics2D){
            Logger.debug("绘制中");
            Graphics2D g2d=(Graphics2D) g;
            g2d.setStroke(new BasicStroke(rootPanel.stroke));
            g2d.setColor(rootPanel.color);
            g2d.fillOval(e.getX(),e.getY(),rootPanel.stroke,rootPanel.stroke);
        }
        g.dispose();
        rootPanel.imageHistory.add(copy);
        lastPoint=e.getPoint();
        rootPanel.repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        Graphics g = rootPanel.imageHistory.peek().getGraphics();
        if(g instanceof  Graphics2D){
            Graphics2D g2d=(Graphics2D) g;
            g2d.setStroke(new BasicStroke(rootPanel.stroke));
            g2d.setColor(rootPanel.color);
            g2d.drawLine(lastPoint.x,lastPoint.y,e.getX(),e.getY());
        }
        g.dispose();
        lastPoint=e.getPoint();
        rootPanel.repaint();
    }
}
